//
//  TKIMTextSearchHelper.m
//  TKIM_首创
//
//  Created by thinkive on 15/12/17.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMTextSearchHelper.h"

@implementation TKIMTextSearchHelper
+(NSArray *)searchText:(NSString *)text fromSoureArray:(NSArray *)sourceArray
{
    if (!text.length||!sourceArray.count) return nil;
    NSMutableArray * resultArray = [NSMutableArray array];
    //[\\u4e00-\\u9fa5]
    //判断输入的字符串中是否有汉字：
    NSString * CNPattern = @"[\\u4e00-\\u9fa5]+";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:CNPattern options:0 error:nil];
    NSTextCheckingResult *firstMatch=[regex firstMatchInString:text options:0 range:NSMakeRange(0, [text length])];
    if (firstMatch) {//输入的文字有汉字  汉字搜索
        for (id<TKIMTextSearchHelperProtocol>item in sourceArray) {
            NSRange range=[item.searchName rangeOfString:text];
            if (range.length) {
                [resultArray addObject:item];
            }
        }
    }else{//输入的文字中无汉字  拼音搜索
        for (id<TKIMTextSearchHelperProtocol>item in sourceArray) {
            NSString * upperName=item.searchPinYinName;
            NSRange range = [upperName rangeOfString:[text uppercaseString]];
            if (range.length) {
                [resultArray addObject:item];
            }
        }
    }
    return resultArray;
}

@end
